//
//  BestMusicTableViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/3.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "BestMusicListViewController.h"
#import "BestMusicHeadTableViewCell.h"
#import "BestMusicListTableViewCell.h"
#import "MusicListViewController.h"

@interface BestMusicListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableDictionary *dataDictionary;
@property(nonatomic,strong)UITableView *bmlTableView;

@end

@implementation BestMusicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"听单";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initBMlListTableView];
    [self getDataWithUrl];
}

/**
 *  初始化mlTableView
 */
- (void)initBMlListTableView
{
    self.bmlTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, FM_Screen_Width, FM_Screen_Height-64-48) style:UITableViewStyleGrouped];
    self.bmlTableView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    self.bmlTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bmlTableView.dataSource = self;
    self.bmlTableView.delegate = self;
    [self.view addSubview:_bmlTableView];
    
    [self.bmlTableView registerClass:[BestMusicHeadTableViewCell class] forCellReuseIdentifier:@"bestHeadCell"];
    [self.bmlTableView registerClass:[BestMusicListTableViewCell class] forCellReuseIdentifier:@"bestMlListCell"];
    
}

// UITableViewDataSource 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.dataDictionary.count != 0) {
        return 2;
    }
        return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return [[self.dataDictionary objectForKey:@"list"] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSString *contentStr = [[self.dataDictionary objectForKey:@"info"] objectForKey:@"intro"];
        CGRect contentRect = [contentStr boundingRectWithSize:CGSizeMake(FM_Screen_Width-FM_Screen_Pts_Width*20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
        return contentRect.size.height+FM_Screen_Pts_Height*50+FM_Screen_Pts_Width*90;
    }
        return FM_Screen_Pts_Width*70+FM_Screen_Pts_Height*20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        // 头
        BestMusicHeadTableViewCell *headCell = [tableView dequeueReusableCellWithIdentifier:@"bestHeadCell"];
        headCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // content内容高度
        NSString *contentStr = [[self.dataDictionary objectForKey:@"info"] objectForKey:@"intro"];
        CGRect contentRect = [contentStr boundingRectWithSize:CGSizeMake(FM_Screen_Width-FM_Screen_Pts_Width*20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
        headCell.height = contentRect.size.height;
        
        headCell.headDic = [self.dataDictionary objectForKey:@"info"];
        return headCell;
    }
    else
    {
        // 列表
        BestMusicListTableViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"bestMlListCell"];
        listCell.bodyDictionary = [[self.dataDictionary objectForKey:@"list"] objectAtIndex:indexPath.row];
        return listCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        MusicListViewController *musicListVC = [[MusicListViewController alloc] init];
        NSDictionary *tempDic = [[self.dataDictionary objectForKey:@"list"] objectAtIndex:indexPath.row];
        musicListVC.musicListDic = tempDic;
        [self.navigationController pushViewController:musicListVC animated:YES];
    }
}

// UITableViewDelegate 协议方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return FM_Screen_Pts_Height*50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0){
        return FM_Screen_Pts_Height*15;
    }
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Width*50)];
        titleView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*10, (FM_Screen_Pts_Width*50-FM_Screen_Pts_Width*15)/2, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15)];
        titleImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
        [titleView addSubview:titleImageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
        titleLabel.text = @"单听推荐";
        titleLabel.font = [UIFont systemFontOfSize:15];
        [titleView addSubview:titleLabel];
        
        return titleView;
    }else{
        return nil;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*15)];
    
    return footerView;
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    self.dataDictionary = [NSMutableDictionary dictionary];
    NSString *parameters = [[self.bestMusicDictionary objectForKey:@"specialId"] stringValue];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?id=%@&device=iPhone",parameters];
    [NetWorkManager POST:url parameters:nil resultData:^(id responseObject) {
        self.dataDictionary = responseObject;
        [self.bmlTableView reloadData];
    } resultRrror:^(NSError *error) {
        NSLog(@"精选:error%@",error);
    }];
}


@end
